package com.ming.closeto.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ming.closeto.R;
import com.ming.closeto.adapter.MovieListAdapter;
import com.ming.closeto.adapter.MusicListAdapter;
import com.ming.closeto.bean.MovieListBean;
import com.ming.closeto.entity.Constant;
import com.ming.closeto.model.MovieListModel;
import com.ming.closeto.ui.MovieMoreActivity;
import com.ming.closeto.utils.LogUtils;
import com.ming.closeto.utils.OkHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MovieFragment extends Fragment {

    private RecyclerView mMovieRecyclerView;
    private MovieListAdapter mMovieListAdapter;
    private List<MovieListBean> mList = new ArrayList<>();

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_movie, null);
        initView(view);
        return view;
    }

    // 初始化view
    public void initView(View view) {
        mMovieRecyclerView = view.findViewById(R.id.mMovieRecyclerView);
        mMovieRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mMovieListAdapter = new MovieListAdapter(getActivity(), mList);
        mMovieRecyclerView.setAdapter(mMovieListAdapter);

        mMovieListAdapter.setOnItemClickListener(new MusicListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                MovieListBean bean = mList.get(position);
                Intent intent = new Intent(getActivity(), MovieMoreActivity.class);
                intent.putExtra("movie", bean.getTitle());
                intent.putExtra("id", bean.getId());
                startActivity(intent);
            }
        });

        initMovieData();
    }

    // 请求电影列表数据
    private void initMovieData() {
        OkHttpUtils.get(Constant.MOVIE_LIST, new OkHttpUtils.OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {
                LogUtils.i(e.toString());
            }

            @Override
            public void onFailure(String msg) {
                LogUtils.i(msg);
            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)) {
                    LogUtils.i("电影列表数据:" + json);
                    loadMovieData(json);
                } else {
                    LogUtils.i("数据为空");
                }
            }
        });
    }

    /**
     * 加载电影数据
     */
    private void loadMovieData(String json) {
        // id title  plot_simple  poster(img)
        MovieListModel model = new Gson().fromJson(json, MovieListModel.class);

        if (model.getResult() == null) {
            Toast.makeText(getActivity(), "这个聚合API晚上没有数据，尴尬!!!", Toast.LENGTH_LONG).show();
            return;
        }
        for (int i=0; i<model.getResult().size(); i++) {
            MovieListBean bean = new MovieListBean();
            // 这里  由于更换接口 bean的变量名与model的变量名有不同
            bean.setTitle(model.getResult().get(i).getMovieName());
            bean.setPlot_simple("0"+ i);
            bean.setPoster(model.getResult().get(i).getPic_url());
            bean.setId(model.getResult().get(i).getMovieId());
            mList.add(bean);

        }
        // 刷新
        mMovieListAdapter.notifyDataSetChanged();
    }

}
